package com.ficus.road.maintain.handler.monitor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/5/23 下午7:33
 */
@Component
public class MonitorHandlerChainStarter {
    @Autowired
    private List<AbstractMonitorHandler> abstractMonitorHandlerList;
    private AbstractMonitorHandler firstHandlerOfMonitorChain;

    @PostConstruct
    void initChainFilter() {
        for (int i = 0; i < abstractMonitorHandlerList.size(); i++) {
            if (i == 0) {
                firstHandlerOfMonitorChain = abstractMonitorHandlerList.get(0);
            } else {
                AbstractMonitorHandler currentHandler = abstractMonitorHandlerList.get(i - 1);
                AbstractMonitorHandler nextHandler = abstractMonitorHandlerList.get(i);
                currentHandler.setNextHandler(nextHandler);
            }
        }
    }

    @Scheduled(cron = "0 0 22 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void exec() {
        firstHandlerOfMonitorChain.filter(null);
    }
}
